<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="说到和动态库查找路径相关的问题，总体上可以分为两类：  第一类：通过源代码编译程序时出现的找不到某个依赖包的问题 第二类：就是在运行程序的时候，明明把那个程序需要的依赖包都已经安装的妥妥的了，可运行的时候人家就告诉你说error while loading shared libraries: libxxx.so.y: cannot open shared object file: No such">
<meta name="keywords" content="linux">
<meta property="og:type" content="article">
<meta property="og:title" content="动态库查找路径及LD_LIBRARY_PATH问题">
<meta property="og:url" content="https://unix2dos.github.io/p/17c0913d.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="说到和动态库查找路径相关的问题，总体上可以分为两类：  第一类：通过源代码编译程序时出现的找不到某个依赖包的问题 第二类：就是在运行程序的时候，明明把那个程序需要的依赖包都已经安装的妥妥的了，可运行的时候人家就告诉你说error while loading shared libraries: libxxx.so.y: cannot open shared object file: No such">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-09-27T15:36:51.204Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="动态库查找路径及LD_LIBRARY_PATH问题">
<meta name="twitter:description" content="说到和动态库查找路径相关的问题，总体上可以分为两类：  第一类：通过源代码编译程序时出现的找不到某个依赖包的问题 第二类：就是在运行程序的时候，明明把那个程序需要的依赖包都已经安装的妥妥的了，可运行的时候人家就告诉你说error while loading shared libraries: libxxx.so.y: cannot open shared object file: No such">
  <link rel="canonical" href="https://unix2dos.github.io/p/17c0913d">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>动态库查找路径及LD_LIBRARY_PATH问题 | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/17c0913d.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">动态库查找路径及LD_LIBRARY_PATH问题

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-08-17 13:52:59" itemprop="dateCreated datePublished" datetime="2019-08-17T13:52:59+08:00">2019-08-17</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-27 23:36:51" itemprop="dateModified" datetime="2019-09-27T23:36:51+08:00">2019-09-27</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/" itemprop="url" rel="index"><span itemprop="name">2-linux系统</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/系统/" itemprop="url" rel="index"><span itemprop="name">系统</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/17c0913d.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/17c0913d.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>5.6k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>5 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>说到和动态库查找路径相关的问题，总体上可以分为两类：</p>
<ul>
<li>第一类：通过源代码编译程序时出现的找不到某个依赖包的问题</li>
<li>第二类：就是在运行程序的时候，明明把那个程序需要的依赖包都已经安装的妥妥的了，可运行的时候人家就告诉你说<code>error while loading shared libraries: libxxx.so.y: cannot open shared object file: No such file or directory</code>。</li>
</ul>
<a id="more"></a>

<h3 id="1-源代码安装程序找不到依赖库"><a href="#1-源代码安装程序找不到依赖库" class="headerlink" title="1. 源代码安装程序找不到依赖库"></a>1. 源代码安装程序找不到依赖库</h3><p>通过源码包安装程序时，主要用到了“三大步”策略：configure、make和make install 。出问题最多的就是在configure阶段，很多初学者由于不知道configure的那么多参数该怎么用，所以往往为了省事，一句简单的“./configure”下去，百分之八九十都能成功，可问题往往就出在剩下的百分之十几上面了。</p>
<p>在安装的configure阶段，为了检测安装安装环境是否满足，通常情况下都是通过一个叫做<code>pkg-config</code>的工具来检测它需要依赖的动态库是否存在，这个工具我们在上一篇博文已经认识过了。pkg-config通常情况都是位于/usr/bin目录下，是个可执行程序。在configure阶段，通常都会用pkg-config来判断所依赖的动态库是否存在。现在问题就是，这个工具是如何判断的呢？它的依据是什么？当这两个问题弄明白了，真相也就大白了。</p>
<p> 一般当我们安装完某个程序后，如果它提供了动态库的功能，在源码中都会有一个或多个以pc结尾的文件，当执行完make install后这些pc文件拷贝到${prefix}/lib/pkgconfig这个目录里，这里的prefix就是我们在configure阶段时通过配置参数–prefix指定的，缺省情况这个值就是/usr/local，所以这些pc文件最终会被拷贝到/usr/local/lib/pkgconfig目录下。可能有人会问，这些pc文件有啥用呢？我们随便打开一个来瞅瞅：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">[root@localhost ~]# cat /usr/local/lib/pkgconfig/librtmp.pc</span><br><span class="line">prefix=/usr/local</span><br><span class="line">exec_prefix=$&#123;prefix&#125;</span><br><span class="line">libdir=$&#123;exec_prefix&#125;/lib</span><br><span class="line">incdir=$&#123;prefix&#125;/include</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Name: librtmp</span><br><span class="line">Description: RTMP implementation</span><br><span class="line">Version: v2.3</span><br><span class="line">Requires: libssl,libcrypto</span><br><span class="line">URL: http://rtmpdump.mplayerhq.hu</span><br><span class="line">Libs: -L$&#123;libdir&#125; -lrtmp -lz</span><br><span class="line">Cflags: -I$&#123;incdir&#125;</span><br></pre></td></tr></table></figure>

<p>跟我们configure阶段相关的主要集中在Libs和Cflags两项上面，如果你此时再执行下面这两条命令，就全明白了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# pkg-config --cflags librtmp</span><br><span class="line">-I/usr/local/include</span><br><span class="line">[root@localhost ~]# pkg-config --libs librtmp</span><br><span class="line">-L/usr/local/lib -lrtmp -lz -lssl -lcrypto</span><br></pre></td></tr></table></figure>

<p>也就是说，pkg-config把我们以前需要在Makefile里指定编译和链接时所需要用到的参数从手工硬编码的模式变成了自动完成，节约了多少跨平台移植的兼容性问题。</p>
<h5 id="1-1-安装了找不到依赖库的原因"><a href="#1-1-安装了找不到依赖库的原因" class="headerlink" title="1.1 安装了找不到依赖库的原因"></a>1.1 安装了找不到依赖库的原因</h5><p>假如说，如果我们将要的编译的软件包依赖librtmp这个动态库，那么此时在我系统上这个检测就算通过了。当然这只是第一步，检测过了不一定兼容，这里我们只讨论能不能找到依赖库的问题。好了，如果说找不到某个库该怎么办。前提是你确确实实已经安装了它需要的库，不用多想，原因只有一个，pkg-config找不到这个与这个库对应的pc文件。</p>
<p>为什么会找不到呢，原因又有两点：</p>
<p>1、pkg-config搜索了所有它认为合适的目录都没找着这个库对应的pc文件的下落；</p>
<p>2、这个库在发布时根本就没有提供它的pc文件。</p>
<blockquote>
<p>那么pkg-config的查找路径是哪里？</p>
</blockquote>
<p>pkg-config较老的版本里，缺省情况下会到/usr/lib/pkgconfig、/usr/loca/lib/pkgconfig、/usr/share/pkgconfig等目录下去搜索pc文件，据我所知在0.23以及之后的版本里pkg-config的源码里已经没有关于缺省搜索路径的任何硬编码的成分了，取而代之的是，当你看pkg-config的man手册时会有下面一段话：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">pkg-config retrieves information about packages from special metadata files. These files are  named  after the  package,  with  the extension .pc.</span><br><span class="line">By default, pkg-config looks in the directory ___prefix___/lib/pkgconfig for these files; it will also look in the colon-separated (on Windows, semicolon-separated) list of directories specified by the PKG_CONFIG_PATH environment variable.</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">PKG_CONFIG_PATH</span><br><span class="line">    A colon-separated (on Windows, semicolon-separated) list of directories to search  for  .pc  files. The  default directory will always be searched after searching the path; the default is ___libdir___/pkg-config:___datadir___/pkgconfig where libdir is the libdir where pkg-config and  datadir  is  the  datadir where pkg-config was installed.</span><br></pre></td></tr></table></figure>

<p>  上面的prefix、libdir和datadir，就是安装pkg-config时被设定好的，具体情况是：</p>
<ul>
<li><p>如果你是通过yum和rpm包安装的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">prefix=/usr</span><br><span class="line">libdir=<span class="variable">$&#123;prefix&#125;</span>/lib=/usr/lib</span><br><span class="line">datadir=<span class="variable">$&#123;prefix&#125;</span>/share=/usr/share</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果你是通过源码包安装的，且没有指定prefix的值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">prefix=/usr/<span class="built_in">local</span></span><br><span class="line">libdir=<span class="variable">$&#123;prefix&#125;</span>/lib=/usr/<span class="built_in">local</span>/lib</span><br><span class="line">datadir=<span class="variable">$&#123;prefix&#125;</span>/share=/usr/<span class="built_in">local</span>/share</span><br></pre></td></tr></table></figure>

</li>
</ul>
<p>pkg-config在查找对应软件包的信息时的缺省搜索路径已经很清楚了，就是是<code>${libdir}/pkgconfig</code>和<code>${datadir}/pkgconfig</code>。如果你软件包对应的pc文件都不在这两个目录下时，pkg-config肯定找不到。既然原因都已经找到了，那解决办法也就多种多样了。</p>
<h5 id="1-2-解决找不到库的问题-PKG-CONFIG-PATH"><a href="#1-2-解决找不到库的问题-PKG-CONFIG-PATH" class="headerlink" title="1.2 解决找不到库的问题(PKG_CONFIG_PATH)"></a>1.2 解决找不到库的问题(PKG_CONFIG_PATH)</h5><ul>
<li><p>我们可以在安装我们那个被依赖的软件包时，在configure阶段用–prefix参数把安装目录指定到/usr目录下；</p>
</li>
<li><p>也可以按照上面说的，通过一个名叫<code>PKG_CONFIG_PATH</code>的环境变量来向pkg-config指明我们自己的pc文件所在的路径，不过要注意的是<code>PKG_CONFIG_PATH</code>所指定的路径优先级比较高，pkg-config会先进行搜索，完了之后才是去搜索缺省路径。</p>
</li>
</ul>
<p>前者的优点是以后再通过源码安装软件时少了不少麻烦，缺点是用户自己的软件包和系统软件混到一起不方便管理，所以实际使用中，后者用的要多一些。如下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> PKG_CONFIG_PATH=/your/<span class="built_in">local</span>/path:<span class="variable">$PKG_CONFIG_PATH</span></span><br></pre></td></tr></table></figure>

<p>  然后，在configure时就绝对没问题了。</p>
<h3 id="2-程序运行时出现libxxx-so-y-gt-not-found"><a href="#2-程序运行时出现libxxx-so-y-gt-not-found" class="headerlink" title="2. 程序运行时出现libxxx.so.y =&gt; not found"></a>2. 程序运行时出现libxxx.so.y =&gt; not found</h3><h5 id="2-1-ldd-查看依赖的动态库"><a href="#2-1-ldd-查看依赖的动态库" class="headerlink" title="2.1 ldd 查看依赖的动态库"></a>2.1 ldd 查看依赖的动态库</h5><p>用<code>ldd 可执行程序名</code>可以查看一个软件启动时所依赖的动态库，如果输出项有“libxxx.so.y=&gt; not found”一项，你这个软件100%运行不起来。我们来做个试验：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]<span class="comment"># echo $LD_LIBRARY_PATH    //嘛也没有</span></span><br><span class="line">[root@localhost ~]<span class="comment"># ldd /usr/local/bin/ffmpeg</span></span><br><span class="line">........</span><br><span class="line">libmp3lame.so.0 =&gt; /usr/<span class="built_in">local</span>/lib/libmp3lame.so.0 (0x0088c000)</span><br><span class="line">libfaac.so.0 =&gt; /usr/<span class="built_in">local</span>/lib/libfaac.so.0 (0x00573000)</span><br><span class="line">........</span><br></pre></td></tr></table></figure>

<p>我的系统里没有设置<code>LD_LIBRARY_PATH</code>环境变量，现在我们把其中的一个库<code>libmp3lame.so.0</code>从<code>/usr/loca/lib</code>下移动到/opt目录里，并执行ldconfig，让<code>libmp3lame.so.0</code>彻底从<code>/etc/ld.so.cache</code>里面消失。其实<code>libmp3lame.so.0</code>只是<code>libmp3lame.so.0.0.0</code>的一个符号链接，我们真正需要移动的是后者.</p>
<p>完了之后再执行ldd /usr/local/bin/ffmpeg时结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]<span class="comment"># ldd /usr/local/bin/ffmpeg</span></span><br><span class="line">........</span><br><span class="line">libmp3lame.so.0 =&gt; not found    //果然Not found 了</span><br><span class="line">libfaac.so.0 =&gt; /usr/<span class="built_in">local</span>/lib/libfaac.so.0 (0x004a4000)</span><br><span class="line">........</span><br><span class="line"></span><br><span class="line">[root@localhost ~]<span class="comment"># ffmpeg --help</span></span><br><span class="line">ffmpeg: error <span class="keyword">while</span> loading shared libraries: libmp3lame.so.0: cannot open shared object file: No such file or directory  //此时ffmpeg当然运行不起来</span><br></pre></td></tr></table></figure>



<h5 id="2-2-LD-LIBRARY-PATH"><a href="#2-2-LD-LIBRARY-PATH" class="headerlink" title="2.2 LD_LIBRARY_PATH"></a>2.2 LD_LIBRARY_PATH</h5><p>我们来试试LD_LIBRARY_PATH，看看好使不：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost opt]<span class="comment"># export LD_LIBRARY_PATH=/opt:$LD_LIBRARY_PATH</span></span><br><span class="line">[root@localhost opt]<span class="comment"># ldd /usr/local/bin/ffmpeg</span></span><br><span class="line">........</span><br><span class="line">libmp3lame.so.0 =&gt; not found           //纳尼？？！！！</span><br><span class="line">libfaac.so.0 =&gt; /usr/<span class="built_in">local</span>/lib/libfaac.so.0 (0x00124000)</span><br><span class="line">........</span><br></pre></td></tr></table></figure>

<p>还记得上面提到了软链接么，<code>libmp3lame.so.0</code>就是<code>libmp3lame.so.0.0.0</code>的软链接，这是动态库的命名规范的一种公约，我们只要在/opt/目录下建立一个名为<code>libmp3lame.so.0</code>的到<code>/opt/libmp3lame.so.0.0.0</code>的软链接就OK了：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost opt]<span class="comment"># ln -s libmp3lame.so.0.0.0 libmp3lame.so.0</span></span><br><span class="line">[root@localhost opt]<span class="comment"># ldd /usr/local/bin/ffmpeg</span></span><br><span class="line">........</span><br><span class="line">libmp3lame.so.0 =&gt; /opt/libmp3lame.so.0 (0x00767000)   //终于圆满了:)</span><br><span class="line">libfaac.so.0 =&gt; /usr/<span class="built_in">local</span>/lib/libfaac.so.0 (0x006e8000)</span><br><span class="line">........</span><br></pre></td></tr></table></figure>

<h3 id="3-总结"><a href="#3-总结" class="headerlink" title="3. 总结"></a>3. 总结</h3><p>针对动态库路径查找的种种问题，无非就这么两大类，关键是找对原因，对症下药，方能药到病除。</p>
<ul>
<li>PKG_CONFIG_PATH从字面意思上翻译，就是“软件包的配置路径”，这不很明显了么，编译软件时如果出现找不到所依赖的动态库时都全靠PKG_CONFIG_PATH了；</li>
<li>LD_LIBRARY_PATH也很直白了“装载器的库路径”，LD是Loader的简写，在Linux系统启动一个程序的过程就叫做装载，一个程序要执行时它或多或少的会依赖一些动态库(静态编译的除外)。</li>
</ul>
<h3 id="4-参考资料"><a href="#4-参考资料" class="headerlink" title="4. 参考资料"></a>4. 参考资料</h3><ul>
<li><a href="http://blog.chinaunix.net/uid-23069658-id-4028681.html" target="_blank" rel="noopener">http://blog.chinaunix.net/uid-23069658-id-4028681.html</a></li>
<li><a href="https://prefetch.net/articles/linkers.badldlibrary.html" target="_blank" rel="noopener">https://prefetch.net/articles/linkers.badldlibrary.html</a></li>
</ul>

    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/linux/" rel="tag"># linux</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/937317d6.html" rel="next" title="为iterm2设置shadowsocks代理">
                  <i class="fa fa-chevron-left"></i> 为iterm2设置shadowsocks代理
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/fae4c74c.html" rel="prev" title="golang的websocket的使用">
                  golang的websocket的使用 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-源代码安装程序找不到依赖库"><span class="nav-number">1.</span> <span class="nav-text">1. 源代码安装程序找不到依赖库</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-安装了找不到依赖库的原因"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.1 安装了找不到依赖库的原因</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-2-解决找不到库的问题-PKG-CONFIG-PATH"><span class="nav-number">1.0.2.</span> <span class="nav-text">1.2 解决找不到库的问题(PKG_CONFIG_PATH)</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-程序运行时出现libxxx-so-y-gt-not-found"><span class="nav-number">2.</span> <span class="nav-text">2. 程序运行时出现libxxx.so.y =&gt; not found</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-1-ldd-查看依赖的动态库"><span class="nav-number">2.0.1.</span> <span class="nav-text">2.1 ldd 查看依赖的动态库</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-2-LD-LIBRARY-PATH"><span class="nav-number">2.0.2.</span> <span class="nav-text">2.2 LD_LIBRARY_PATH</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-总结"><span class="nav-number">3.</span> <span class="nav-text">3. 总结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-参考资料"><span class="nav-number">4.</span> <span class="nav-text">4. 参考资料</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/17c0913d.html";
    this.page.identifier = "p/17c0913d.html";
    this.page.title = '动态库查找路径及LD_LIBRARY_PATH问题';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
